﻿using EAS.Common.ModUtils;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EAS.Content.Projectiles
{
    public class Beam : ModProjectile
    {
        public Player Owner => Main.player[Projectile.owner];

        public ref float TimerForCharging => ref Projectile.ai[0];

        public ref float Beams => ref Projectile.ai[1];
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.TrailCacheLength[Projectile.type] = 6;
            ProjectileID.Sets.TrailingMode[Projectile.type] = 0;
            ProjectileID.Sets.MinionShot[Projectile.type] = true;
        }

        public override void SetDefaults()
        {
            Projectile.width = Projectile.height = 20;
            Projectile.friendly = true;
            Projectile.ignoreWater = true;
            Projectile.tileCollide = false;
            Projectile.timeLeft = 360;
            Projectile.DamageType = DamageClass.Summon;
        }
        public override void AI()
        {
            Beams = Main.rand.Next(2);
            Beams = (Beams == 0f) ? DustID.YellowStarDust : DustID.YellowStarDust;           
            SpawnDust();
            DustTrail(); 
            TargetNPC();
            Projectile.damage = 45;
            Lighting.AddLight(Projectile.Center, Color.White.ToVector3());
            Projectile.rotation += MathHelper.ToRadians(Projectile.velocity.X);
        }     
        public void SpawnDust()
        {
            if (Projectile.localAI[0] == 0f)
            {
                for (int i = 0; i < 45; i++) 
                {
                    float angle = MathHelper.TwoPi / 45f * i;
                    Vector2 velocity = angle.ToRotationVector2() * 4f;
                    Dust circleSpawn = Dust.NewDustPerfect(Projectile.Center + velocity * 3.5f, (int)Beams, velocity, 0, default, 1.25f);
                    circleSpawn.noGravity = true;
                }
            }
            Projectile.localAI[0] = 1f;
        }

        public void DustTrail()
        {
            Dust dustTrail = Dust.NewDustPerfect(Projectile.Center, (int)Beams);
            dustTrail.velocity = Main.rand.NextVector2Circular(2f, 2f);
            dustTrail.noGravity = true;
            dustTrail.scale = 2f;
            Projectile.netUpdate = true;
        }

        public void TargetNPC() 
        {
            NPC potentialTarget = Projectile.Center.MinionHoming(1700f, Owner);
            if (potentialTarget != null)
            {
                Projectile.velocity = Vector2.Lerp(Projectile.velocity, (potentialTarget.Center - Projectile.Center).SafeNormalize(Vector2.Zero) * 30f, TimerForCharging);
                TimerForCharging += 0.001f;
                TimerForCharging = (TimerForCharging > 1f) ? 1f : TimerForCharging;
            }
            Projectile.netUpdate = true;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {         
            target.AddBuff(BuffID.OnFire3, 240);
        }

        
        public override bool PreDraw(ref Color lightColor)
        {
            AccUtil.DrawAfterimagesCentered(Projectile, ProjectileID.Sets.TrailingMode[Projectile.type], lightColor, 1);
            return false;
        }
        public override bool? CanDamage() => true;
    }
}